package com.beikai.springbootdemo.config.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

/**
 * @ClassName DataSourceConfiguration
 * @Description TODO
 * @Author Admin
 * @Date 2018/11/18 19:04
 * @Version 1.0
 * springboot 相对于 springmvc 减少了许多的xml文件的配置,对于这些配置,是放在代码中进行配置的
 * 因为springboot本身封装了大量的spring配置
 *
 *  要了解ioc和aop  关键的部分
 *  ioc 就是一个容器 去接收很多的bean (运行时)当需要这些bean的时候,从容器中获取(动态的注入)
 *
 *@Configuration  使用这个注解是告诉spring容器 在初始化的时候 找到这个bean 并初始化
 *
 **/
@Configuration
// 配置mybatis mapper的扫描路径
@MapperScan("com.beikai.springbootdemo.dao")
public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String jdbcDriver;
    @Value("${jdbc.url}")
    private String jdbcUrl;
    @Value("${jdbc.username}")
    private String jdbcUsername;
    @Value("${jdbc.password}")
    private String jdbcPassword;

    @Bean(name = "dataSource")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass(jdbcDriver);
        comboPooledDataSource.setJdbcUrl(jdbcUrl);
        comboPooledDataSource.setUser(jdbcUsername);
        comboPooledDataSource.setPassword(jdbcPassword);
        // 关闭连接后 不自动commit
        comboPooledDataSource.setAutoCommitOnClose(false);

        return comboPooledDataSource;
    }
}
